package com.zxy.day0624_util.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: lenovo
 * @Date: 2021/9/27 20:44
 * @Description:
 */
public class DateUtil {
    public DateUtil() {
    }

    public static String getStrFromDate(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }

    public static String getStrFromDate(Date date, String param) {
        return (new SimpleDateFormat(param)).format(date);
    }

    public static Date getDateFromStr(String param) throws Exception {
        return (new SimpleDateFormat("yyyy-MM-dd")).parse(param);
    }

    public static int getMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        int nowYear = instance.get(1);
        int nowMonth = instance.get(2);
        instance.setTime(date);
        int year = instance.get(1);
        int month = instance.get(2);
        return 12 * (nowYear - year) + (nowMonth - month);
    }

    public static int getAge(Date date) {
        Calendar instance = Calendar.getInstance();
        int nowYear = instance.get(1);
        int nowMonth = instance.get(2);
        int nowDay = instance.get(5);
        instance.setTime(date);
        int year = instance.get(1);
        int month = instance.get(2);
        int day = instance.get(5);
        int age = nowYear - year;
        if (nowMonth < month) {
            --age;
        }

        if (nowMonth == month && nowDay < day) {
            --age;
        }

        return age;
    }

    public static int getBirthAge(Date birthday) {
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        Month month = now.getMonth();
        int dayOfMonth = now.getDayOfMonth();
        Instant instant = birthday.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime of = instant.atZone(zoneId).toLocalDateTime();
        int year1 = of.getYear();
        Month month1 = of.getMonth();
        int dayOfMonth1 = of.getDayOfMonth();
        int age = year - year1;
        now.compareTo(of);
        if (month.compareTo(month1) < 0) {
            return age--;
        } else {
            return month.compareTo(month1) == 0 && dayOfMonth < dayOfMonth1 ? age-- : age;
        }
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(instance.get(1), instance.get(2), 1, 0, 0, 0);
        return instance.getTime();
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(2, 1);
        instance.set(instance.get(1), instance.get(2), 1, 0, 0, 0);
        instance.add(13, -1);
        return instance.getTime();
    }

    public static Date offsetDate(int days) {
        Calendar instance = Calendar.getInstance();
        instance.add(5, days);
        return instance.getTime();
    }

    public static String getCurrentSeason() {
        Calendar instance = Calendar.getInstance();
        int month = instance.get(2) + 1;
        System.out.println(month);
        if (month < 9 && month > 2) {
            return month < 6 ? Season.SPRING.getName() : Season.SUMMER.getName();
        } else {
            return month >= 9 && month < 12 ? Season.AUTUMN.getName() : Season.WINTER.getName();
        }
    }

    public static String getDisplayTime(Date date) {
        long t1 = date.getTime();
        long t2 = (new Date()).getTime();
        long t = t2 - t1;
        long minute = t / 1000L / 60L;
        if (t2 < t1) {
            return "未来时间";
        } else if (minute / 60L / 24L / 30L / 12L >= 1L) {
            return minute / 60L / 24L / 30L / 12L + "年前";
        } else if (minute / 60L / 24L / 30L >= 1L) {
            return minute / 60L / 24L / 30L + "月前";
        } else if (minute / 60L / 24L >= 1L) {
            return minute / 60L / 24L + "天前";
        } else if (minute / 60L >= 1L) {
            return minute / 60L + "小时前";
        } else {
            return minute > 5L ? minute + "分钟前" : "刚刚";
        }
    }
}
